GitHub 您所在的位置:网站首页 vba iframe GitHub

GitHub

2023-03-16 03:30| 来源: 网络整理| 查看: 265

TinySeleniumVBA

A tiny Selenium wrapper written in pure VBA.

🇯🇵日本語のREADMEはこちら

🇧🇷Versão em Português

✨ Features No installation: Everyone even who doesn't have permissions to install can automate browser operations. Useful helper Methods: FindElement(s)By*, Get/Set value to form, click and more. Open spec: Basically this wrapper is just a HTTP client of WebDriver server. Learning this wrapper equals to learning WebDriver. https://www.w3.org/TR/webdriver/ 📦 Setup

Set reference to Microsoft Scripting Runtime

Add WebDriver.cls, WebElement.cls, Capabilities.cls and JsonConverter.bas to your VBA Project

Latest (v0.1.3): https://github.com/uezo/TinySeleniumVBA/releases/tag/v0.1.3

Download WebDriver (driver and browser should be the same version)

Edge: https://developer.microsoft.com/ja-jp/microsoft-edge/tools/webdriver/ Chrome: https://chromedriver.chromium.org/downloads 🪄 Usage Public Sub main() ' Start WebDriver (Edge) Dim Driver As New WebDriver Driver.Edge "path\to\msedgedriver.exe" ' Open browser Driver.OpenBrowser ' Navigate to Google Driver.Navigate "https://www.google.co.jp/?q=selenium" ' Get search textbox Dim searchInput Set searchInput = Driver.FindElement(By.Name, "q") ' Get value from textbox Debug.Print searchInput.GetValue ' Set value to textbox searchInput.SetValue "yomoda soba" ' Click search button Driver.FindElement(By.Name, "btnK").Click ' Refresh - you can use Execute with driver command even if the method is not provided Driver.Execute Driver.CMD_REFRESH End Sub 🐙 BrowserOptions

Use Capabilities to configure browser options. This is an example to launch browser as headless (invisible) mode.

' Start web driver Dim Driver As New WebDriver Driver.Chrome "C:\path\to\chromedriver.exe" ' Configure Capabilities Dim cap As Capabilities Set cap = Driver.CreateCapabilities() cap.AddArgument "--headless" ' Use SetArguments if you want to add multiple arguments ' cap.SetArguments "--headless --xxx -xxx" ' Show Capabilities as JSON for debugging Debug.Print cap.ToJson() ' Open browser Driver.OpenBrowser cap

See also the sites below to understand the spec of Capabilities for each browser.

Chrome: https://chromedriver.chromium.org/capabilities Edge: https://docs.microsoft.com/en-us/microsoft-edge/webdriver-chromium/capabilities-edge-options ⚡️ Execute JavaScript

Use ExecuteScript() to execute JavaScript on the browser.

' Start web driver Dim Driver As New WebDriver Driver.Chrome "C:\path\to\chromedriver.exe" ' Open browser Driver.OpenBrowser ' Navigate to Google Driver.Navigate "https://www.google.co.jp/?q=liella" ' Show alert Driver.ExecuteScript "alert('Hello TinySeleniumVBA')" ' === Use breakpoint to CLOSE ALERT before continue === ' Pass argument Driver.ExecuteScript "alert('Hello ' + arguments[0] + ' as argument')", Array("TinySeleniumVBA") ' === Use breakpoint to CLOSE ALERT before continue === ' Pass element as argument Dim searchInput Set searchInput = Driver.FindElement(By.Name, "q") Driver.ExecuteScript "alert('Hello ' + arguments[0].value + ' ' + arguments[1])", Array(searchInput, "TinySeleniumVBA") ' === CLOSE ALERT and continue === ' Get return value from script Dim retStr As String retStr = Driver.ExecuteScript("return 'Value from script'") Debug.Print retStr ' Get WebElement as return value from script Dim firstDiv As WebElement Set firstDiv = Driver.ExecuteScript("return document.getElementsByTagName('div')[0]") Debug.Print firstDiv.GetText() ' Get complex structure as return value from script Dim retArray retArray = Driver.ExecuteScript("return [['a', '1'], {'key1': 'val1', 'key2': document.getElementsByTagName('div'), 'key3': 'val3'}]") Debug.Print retArray(0)(0) ' a Debug.Print retArray(0)(1) ' 1 Debug.Print retArray(1)("key1") ' val1 Debug.Print retArray(1)("key2")(0).GetText() ' Inner Text Debug.Print retArray(1)("key2")(1).GetText() ' Inner Text Debug.Print retArray(1)("key3") ' val3 ❤️ Thanks

VBA-JSON by Tim Hall, JSON converter for VBA helps me a lot to make HTTP client and this awesome library is included in the release under its license. Thank you!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有